
 <#-- 
 
 crea una clase para c# para la tabla  ${table}...
 Author : Luis Molina...
 
 -->
<#-- variables used internally by the generator... -->
<#assign nameFile="aspx">
<#assign extensionFile="cs">
<#assign languageGenerated="c#">
<#assign description="c#">
<#assign targetDirectory="clases">
<#assign appliesToAllTables="true">
<#-- END variables used internally by the generator... -->
 <?php
require_once 'dbclass.php';
 
class ${table} 
{
	<#list table.getFields() as field>
		 <#-- segun el tipo de campo...   -->
		 <#switch field.getType()>
			  <#case "_int">
		var $${field};
			  <#break>
			  <#case "_string">
		var $${field};
			  <#break>
			  <#case "_date">
		var $${field};
			  <#break>
			  <<#case "_boolean">
		var $${field};
			  <#break>
			  <#default>var $${field};
		</#switch>  
	</#list>
	
    public function __construct()
    {
//      $this->db = Mysql::singleton();
    }
	
    function Delete(${table.getListParametersKeyField()})
    {
        $db = new dbclass();
        try { 
        $sqlt = " delete from ${table} ";
        $sqlt = $sqlt." where 1!=0";  
        
        <#assign count=0>	
        <#list table.getFields() as field>
            <#if (field.getisKey())>
                <#if (field.getType()=="string")>
        $sqlt = $sqlt." and ${field}='".$${field}x';
                <#else>
        $sqlt = $sqlt." and ${field}=".$${field}x;
                </#if>
            </#if>
         <#assign count = count+1>
		</#list>
           $result= $db->query($sqlt);
           }
           catch (Exception $e)
           {
               // Argument is optional, no "When" keyword 
           }
     }
	

    function Insert($obj${table})
    {
        $db = new dbclass();
        try
        {
        $sqlt = " insert into ${table} (${table.getlistWithAllFieldsExceptKeys()})"; 
        $sqlt = $sqlt." values (";
			<#assign tot=0>	
			<#list table.getFields() as field> 
			<#assign tot = tot+1>
			</#list>  
			<#assign count=0>	
			<#list table.getFields() as field> 
			 <#assign count = count+1>
			<#if (count == tot)><#if (field.getType()=="_string")>$sqlt = "'".$sqlt.$obj${table}->${field}."' " ;<#else>$sqlt = $sqlt.$obj${table}->${field}." " ;</#if><#else><#if (field.getType()=="_string")>$sqlt = "'".$sqlt.$obj${table}->${field}."'," ;<#else>$sqlt = $sqlt.$obj${table}->${field}."," ;</#if></#if>
       	   </#list>  
			$sqlt = $sqlt." )";
            $result= $db->query($sqlt);
            }
            catch (Exception $e)
            {
                // Argument is optional, no "When" keyword 
            }
            return $result;
       }
	
// devuelve una lista de ${table}
 
 	// devuelve una lista de ${table}
 
 		function  getList()
		{
            $db = new dbclass();
            try
            {
            $sqlt = "select ${table.getListWithAllFields()} from ${table}";
            $result= $db->query($sqlt);
			$lista=$db->fetch_rows($result);
            }
            catch (Exception $e)
            {
                 
            }
            return $lista;
       }		
	
 
 
 		function get${table}(${table.getListParametersKeyField()})
		{
            $db = new dbclass();
            try
            {
            $sqlt = "select * from ${table}";
            $sqlt = $sqlt." where 1!=0";  
            <#list table.getFields() as field>
              <#if (field.getisKey())>   
                     <#-- segun el tipo de campo...   -->
			$sqlt = $sqlt." and ${field}=".$${field};
               </#if>
            </#list>
            $result= $db->query($sqlt);
            if (mysql_num_rows($result)>0)
            {
              <#list table.getFields() as field>
                         <#-- segun el tipo de campo...   -->
                         <#switch field.getType()>
                                  <#case "_int">
             $this->${field} =  mysql_result($result, 0, "${field}");  
                                     <#break>
                                  <#case "_string">
             $this->${field} = mysql_result($result, 0, "${field}");  	
                                     <#break>
                                  <#case "_date">
             $this->${field} = mysql_result($result, 0, "${field}");  
                                     <#break>
                                  <#case "_boolean">
            $this->${field} = mysql_result($result, 0, "${field}");  
                                     <#break>
                                  <#default>
             $this->${field} = mysql_result($result, 0, "${field}");    					
                        </#switch>  
                </#list>		 
               }
            }
            catch (Exception $e)
            {
                // Argument is optional, no "When" keyword 
            }
             return  $this ;
       }


		function Update($obj${table})
		{
            $db = new dbclass();
            try
            {
            $sqlt =" update ${table} set";
		<#assign count=0>		
		<#list table.getFields() as field>
		 	 <#-- segun el tipo de campo...   -->
			 <#switch field.getType()>
				  <#case "_int">
			$sqlt = $sqlt."${field}=" + obj${table}.${field}+ "', " ;
					 <#break>
				  <#case "_string">
			$sqlt = $sqlt.".${field}='" + obj${table}.${field}+ "', "; 
					 <#break>
				  <#case "_date">
			$sqlt = $sqlt.".${field}='" + sf.fechaIso(obj${table}.${field}) + "', " ;
					 <#break>
				  <#case "_boolean">
			$sqlt = $sqlt.".${field}=" + sf.Bool(obj${table}.${field}) + "', ";
					 <#break>
				  <#default>
			$sqlt = $sqlt.".${field}='" + obj${table}.${field}+ "', " ;
			</#switch> 
		 <#assign count = count+1>
		</#list>
            $result= $db->query($sqlt);
            }   
            catch (Exception $e)
            {
                  return false;
            }
            return true;
       }

// comprueba si existe

 		function exists(${table.getListParametersKeyField()} )
		{
            $db = new dbclass();
			$retorno=false;
            try
            {
            $sqlt = "select * from ${table}";
            $sqlt = $sqlt." where 1!=0";  
            <#list table.getFields() as field>
                <#if (field.getisKey())>
                     <#-- segun el tipo de campo...   -->
                     <#switch field.getType()>
                              <#case "_int">
            $sqlt = $sqlt." and ${field}=" + this.${field};
                                 <#break>
                              <#case "_string">
            $sqlt = $sqlt." and ${field}='" + this.${field} + "'";
                                 <#break>
                              <#case "_date">
            $sqlt = $sqlt." and ${field}='" + this.${field} + "'";
                                 <#break>
                              <#case "_boolean">
            $sqlt = $sqlt." and ${field}=" + this.${field};
                                 <#break>
                              <#default>
            $sqlt = $sqlt." and ${field}='" + this.${field} + "'";
                    </#switch>  
              </#if>
            </#list>
            $result= $db->query($sqlt);
            if (mysql_num_rows($result)>0)  
				$retorno= true;
            }
            catch (Exception $e)
            {
                // Argument is optional, no "When" keyword 
            }
            return $retorno;
		}
<#assign x=0>
<#list table.getRelations() as relation>
    <#assign x = x+1>
        function get${table}By${relation.getParentKey()}($${relation.getParentKey()})
        {
            $db = new dbclass();
            try
            {
            $sqlt = "select * from ${relation.getChildTable()}";
            $sqlt = $sqlt." where ${relation.getParentKey()}=" + sf.cadena(${relation.getParentKey()});  
            $result= $db->query($sqlt);
            if (mysql_num_rows($result)>0)
            {
              <#list table.getFields() as field>

                         <#-- segun el tipo de campo...   -->
                         <#switch field.getType()>
                                  <#case "_int">
            $this->${field} = mysql_result($result, 0, "${field}");   
                                     <#break>
                                  <#case "_string">
            $this->${field} = mysql_result($result, 0, "${field}");   		
                                     <#break>
                                  <#case "_date">
            $this->${field} = mysql_result($result, 0, "${field}");   
                                     <#break>
                                  <#case "_boolean">
            $this->${field} = mysql_result($result, 0, "${field}");   
                                     <#break>
                                  <#default>
            $this->${field} = mysql_result($result, 0, "${field}");   			
                        </#switch>  
                </#list>		 
               }
            }
            catch (Exception $e)
            {
                // Argument is optional, no "When" keyword 
            }
            return  obj${table} ;
       }
</#list>
}
?>